package servlet;

import service.MerchantManager;
import service.factory.ManagerFactory;
import util.HttpUtil;
import util.JsonUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.MerchantClient;
import pojo.MerchantDto;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class ShowMerchantsDataServlet extends HttpServlet{
    private MerchantManager manager;

    public ShowMerchantsDataServlet() {
        super();
        manager = ManagerFactory.getMerchantManagerInstance();
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String typeId = req.getParameter("typeId");
		String merchantString = null;
		try {
			merchantString = HttpUtil.getResponseStr("http://ZHA-ITA077-w7:8080/admins/c/all");
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<MerchantDto> correctMerchants = (List<MerchantDto>) JsonUtil.toJsonListObject(merchantString, List.class, MerchantDto.class);
        List<MerchantClient> list = manager.getMerchantByType(typeId, correctMerchants);
        PrintWriter writer = resp.getWriter();
        writer.write(JsonUtil.toJsonString(list));
    }
}
